package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC63 {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if (null == obstacleGrid || obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] != 0) {
            return 0;
        }

        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        dp[0][0] = obstacleGrid[0][0] == 0 ? 1 : 0;
        for (int i = 1; i < m; i++) {
            dp[i][0] = (obstacleGrid[i - 1][0] != 0 || dp[i - 1][0] == 0) ? 0 : 1;
        }
        for (int j = 1; j < n; j++) {
            dp[0][j] = (obstacleGrid[0][j - 1] != 0 || dp[0][j - 1] == 0) ? 0 : 1;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = (obstacleGrid[i][j - 1] == 0 ? dp[i][j - 1] : 0) + (obstacleGrid[i - 1][j] == 0 ? dp[i - 1][j] : 0);
            }
        }

        return dp[m - 1][n - 1];
    }

    public static void main(String[] args) {
        LC63 an = new LC63();
        int[][] arr = {
                {0, 1}
        };
        System.out.println(an.uniquePathsWithObstacles(arr));
    }
}
